<?php
declare(strict_types=1);
namespace App\Controller\WebApi\v1;

use App\Controller\AbstractController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\Di\Annotation\Inject;
use Service\SearchService\Index\StoreCommodityIndex;
use Service\GoodsService\MnGoodsService;
use Service\GoodsService\ChannelGoodsService;
use Service\SearchService\Index\ChannelCommodityIndex;

/**
 * 搜索服务接口数据
 * @author Luoshumai
 * @Controller(prefix="/webapi/v1/search")
 */
class SearchController extends AbstractController
{
    /**
     * @Inject()
     * @var StoreCommodityIndex
     */
    protected $storeCommodity;
    
    /**
     * @Inject()
     * @var ChannelCommodityIndex
     */
    protected $channelCommodity;
    
    /**
     * 店铺c端小程序搜索服务变更接口
     * @RequestMapping(path="storeGoodsChange", methods="post")
     */
    public function storeGoodsChange(RequestInterface $request)
    {
        $mn_sid = (int)$request->input('mn_sid');
        $cid    = (int)$request->input('cid');
        $mn_goods = MnGoodsService::instance()->queryMnGoodsPageList($mn_sid); 
        $searchData = [];
        foreach ($mn_goods as $item) 
        {
            $searchData[] = [
                'id'                 => $item['StoreGoodsID'],
                'goodsId'            => $item['StoreGoodsID'],
                'name'               => $item['GoodsName'],
                'brandName'          => $item['BrandName'],
                'classifyID'         => $item['ClassID'],
                'storeID'            => $mn_sid,
                'channelID'          => $cid,
                'isStoreGoodsActive' => true,
                'isChannelGoodsActive' => true,
                'isTrade'  => true,
            ];
        }
        $message = [
            'datas'  => $searchData,
        ];
        return $this->storeCommodity->addMultiDoc($message);
    }
    
    /**
     * 渠道商品搜索服务变更接口
     * @RequestMapping(path="channelGoodsChange", methods="post")
     */
    public function channelGoodsChange(RequestInterface $request)
    {
        $cid = (int)$request->input('cid');
        $channel_goods = ChannelGoodsService::instance()->queryGoodsPageList($cid);
        $searchData = [];
        foreach ($channel_goods as $item)
        {
            $searchData[] = [
                'ID'        => $item['GoodsID'],
                'goodsId'   => $item['GoodsID'],
                'name'      => $item['GoodsName'],
                'brandName' => $item['BrandName'],
                'ClassID'   => $item['ClassID'],
                'ChannelID' => $cid,
            ];
        }
        $message = [
            'DataList'  => $searchData,
            'DataType'  => '',
        ];
        return $this->channelCommodity->addMultiDoc($message);
    }
    
    /**
     * 店铺商品搜索建议
     * @RequestMapping(path="storeSuggestGoods", methods="post")
     */
    public function storeSuggestGoods(RequestInterface $request)
    {
        $mn_sid = (int)$request->input('mn_sid');
        $keyword = $request->input('keyword');
        return $this->storeCommodity->searchSuggest([
            'mn_sid'=>$mn_sid,
            'keyword'=>$keyword
        ]);
    }
    
    /**
     * 渠道商品搜索建议
     * @RequestMapping(path="channelSuggestGoods", methods="post")
     */
    public function channelSuggestGoods(RequestInterface $request)
    {
        $cid = (int)$request->input('cid');
        $keyword = $request->input('keyword');
        return $this->channelCommodity->searchSuggest($cid,$keyword);
    }

  
    /**
     * 渠道商品搜索建议
     * @RequestMapping(path="allDown", methods="post")
     */
    public function allDown()
    {
        return $this->storeCommodity->deleteAllMultiDoc([
            'datas'=>23
        ]);
    }
}